using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Entity;
using APP.QUANLY.Entity.ConditionForm;

namespace APP.QUANLY.DAL
{
    /// <summary>
    /// Summary description for KHACHHANG.
     /// </summary>
	public class KHACHHANGDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public KHACHHANGDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public KHACHHANGDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT MAKHACHHANG, MABARCODE, HINHANH, CODEKHACHHANG, CMND, MAKHUVUC, MANHOM, MALOAIKHACHHANG, TENKHACHHANG, MASOTHUE, NGUOIDAIDIEN, NGAYSINH, GIOITINH, CHUCVU, DIACHI, DIENTHOAI, FAX, DIDONG, EMAIL, CHECKTPYE, GHICHU, DIACHICONGTY, TENTATKHACHHANG, CONGTY, DIACHIGIAOHOADON, DIACHIGIAOHANG, DIACHIGIAOHANG2, TONGCONGNO, GIOIHANCONGNO, GIOIHANSONGAYNO, NGAYNOGANNHAT, CHIETKHAU, CHECKKHUYENMAI, ISUSE, ISDELETE, NGAYTAO, NGUOITAO, NGAYCAPNHAT, NGUOICAPNHAT FROM KHACHHANG WHERE (MAKHACHHANG = @MAKHACHHANG)";
	        _SQLSelectByKey = query;

            query = @"UPDATE KHACHHANG SET MABARCODE = @MABARCODE, HINHANH = @HINHANH, CODEKHACHHANG = @CODEKHACHHANG, CMND = @CMND, MAKHUVUC = @MAKHUVUC, MANHOM = @MANHOM, MALOAIKHACHHANG = @MALOAIKHACHHANG, TENKHACHHANG = @TENKHACHHANG, MASOTHUE = @MASOTHUE, NGUOIDAIDIEN = @NGUOIDAIDIEN, NGAYSINH = @NGAYSINH, GIOITINH = @GIOITINH, CHUCVU = @CHUCVU, DIACHI = @DIACHI, DIENTHOAI = @DIENTHOAI, FAX = @FAX, DIDONG = @DIDONG, EMAIL = @EMAIL, CHECKTPYE = @CHECKTPYE, GHICHU = @GHICHU, DIACHICONGTY = @DIACHICONGTY, TENTATKHACHHANG = @TENTATKHACHHANG, CONGTY = @CONGTY, DIACHIGIAOHOADON = @DIACHIGIAOHOADON, DIACHIGIAOHANG = @DIACHIGIAOHANG, DIACHIGIAOHANG2 = @DIACHIGIAOHANG2, TONGCONGNO = @TONGCONGNO, GIOIHANCONGNO = @GIOIHANCONGNO, GIOIHANSONGAYNO = @GIOIHANSONGAYNO, NGAYNOGANNHAT = @NGAYNOGANNHAT, CHIETKHAU = @CHIETKHAU, CHECKKHUYENMAI = @CHECKKHUYENMAI, ISUSE = @ISUSE, ISDELETE = @ISDELETE, NGAYTAO = @NGAYTAO, NGUOITAO = @NGUOITAO, NGAYCAPNHAT = @NGAYCAPNHAT, NGUOICAPNHAT = @NGUOICAPNHAT WHERE (MAKHACHHANG = @MAKHACHHANG)";
	        _SQLUpdate = query;

            query = @"INSERT INTO KHACHHANG VALUES (@MAKHACHHANG, @MABARCODE, @HINHANH, @CODEKHACHHANG, @CMND, @MAKHUVUC, @MANHOM, @MALOAIKHACHHANG, @TENKHACHHANG, @MASOTHUE, @NGUOIDAIDIEN, @NGAYSINH, @GIOITINH, @CHUCVU, @DIACHI, @DIENTHOAI, @FAX, @DIDONG, @EMAIL, @CHECKTPYE, @GHICHU, @DIACHICONGTY, @TENTATKHACHHANG, @CONGTY, @DIACHIGIAOHOADON, @DIACHIGIAOHANG, @DIACHIGIAOHANG2, @TONGCONGNO, @GIOIHANCONGNO, @GIOIHANSONGAYNO, @NGAYNOGANNHAT, @CHIETKHAU, @CHECKKHUYENMAI, @ISUSE, @ISDELETE, @NGAYTAO, @NGUOITAO, @NGAYCAPNHAT, @NGUOICAPNHAT)";
	        _SQLInsert = query;

            query = @"DELETE FROM KHACHHANG WHERE (MAKHACHHANG = @MAKHACHHANG)";
	        _SQLDelete = query;

            query = @"SELECT MAKHACHHANG, MABARCODE, HINHANH, CODEKHACHHANG, CMND, MAKHUVUC, MANHOM, MALOAIKHACHHANG, TENKHACHHANG, MASOTHUE, NGUOIDAIDIEN, NGAYSINH, GIOITINH, CHUCVU, DIACHI, DIENTHOAI, FAX, DIDONG, EMAIL, CHECKTPYE, GHICHU, DIACHICONGTY, TENTATKHACHHANG, CONGTY, DIACHIGIAOHOADON, DIACHIGIAOHANG, DIACHIGIAOHANG2, TONGCONGNO, GIOIHANCONGNO, GIOIHANSONGAYNO, NGAYNOGANNHAT, CHIETKHAU, CHECKKHUYENMAI, ISUSE, ISDELETE, NGAYTAO, NGUOITAO, NGAYCAPNHAT, NGUOICAPNHAT FROM KHACHHANG";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(KHACHHANG obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "MAKHACHHANG", obj.MAKHACHHANG, DbType.Guid);
                         DalTools.addDbDataParameter(command, "MABARCODE", obj.MABARCODE, DbType.String);
         DalTools.addDbDataParameter(command, "HINHANH", obj.HINHANH, DbType.Binary);
         DalTools.addDbDataParameter(command, "CODEKHACHHANG", obj.CODEKHACHHANG, DbType.String);
         DalTools.addDbDataParameter(command, "CMND", obj.CMND, DbType.String);
         DalTools.addDbDataParameter(command, "MAKHUVUC", obj.MAKHUVUC, DbType.Int32);
         DalTools.addDbDataParameter(command, "MANHOM", obj.MANHOM, DbType.Int32);
         DalTools.addDbDataParameter(command, "MALOAIKHACHHANG", obj.MALOAIKHACHHANG, DbType.Int32);
         DalTools.addDbDataParameter(command, "TENKHACHHANG", obj.TENKHACHHANG, DbType.String);
         DalTools.addDbDataParameter(command, "MASOTHUE", obj.MASOTHUE, DbType.String);
         DalTools.addDbDataParameter(command, "NGUOIDAIDIEN", obj.NGUOIDAIDIEN, DbType.String);
         DalTools.addDbDataParameter(command, "NGAYSINH", obj.NGAYSINH, DbType.DateTime);
         DalTools.addDbDataParameter(command, "GIOITINH", obj.GIOITINH, DbType.String);
         DalTools.addDbDataParameter(command, "CHUCVU", obj.CHUCVU, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHI", obj.DIACHI, DbType.String);
         DalTools.addDbDataParameter(command, "DIENTHOAI", obj.DIENTHOAI, DbType.String);
         DalTools.addDbDataParameter(command, "FAX", obj.FAX, DbType.String);
         DalTools.addDbDataParameter(command, "DIDONG", obj.DIDONG, DbType.String);
         DalTools.addDbDataParameter(command, "EMAIL", obj.EMAIL, DbType.String);
         DalTools.addDbDataParameter(command, "CHECKTPYE", obj.CHECKTPYE, DbType.Int32);
         DalTools.addDbDataParameter(command, "GHICHU", obj.GHICHU, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHICONGTY", obj.DIACHICONGTY, DbType.String);
         DalTools.addDbDataParameter(command, "TENTATKHACHHANG", obj.TENTATKHACHHANG, DbType.String);
         DalTools.addDbDataParameter(command, "CONGTY", obj.CONGTY, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHIGIAOHOADON", obj.DIACHIGIAOHOADON, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHIGIAOHANG", obj.DIACHIGIAOHANG, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHIGIAOHANG2", obj.DIACHIGIAOHANG2, DbType.String);
         DalTools.addDbDataParameter(command, "TONGCONGNO", obj.TONGCONGNO, DbType.Decimal);
         DalTools.addDbDataParameter(command, "GIOIHANCONGNO", obj.GIOIHANCONGNO, DbType.Decimal);
         DalTools.addDbDataParameter(command, "GIOIHANSONGAYNO", obj.GIOIHANSONGAYNO, DbType.Int32);
         DalTools.addDbDataParameter(command, "NGAYNOGANNHAT", obj.NGAYNOGANNHAT, DbType.DateTime);
         DalTools.addDbDataParameter(command, "CHIETKHAU", obj.CHIETKHAU, DbType.Double);
         DalTools.addDbDataParameter(command, "CHECKKHUYENMAI", obj.CHECKKHUYENMAI, DbType.Int32);
         DalTools.addDbDataParameter(command, "ISUSE", obj.ISUSE, DbType.Boolean);
         DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean);
         DalTools.addDbDataParameter(command, "NGAYTAO", obj.NGAYTAO, DbType.DateTime);
         DalTools.addDbDataParameter(command, "NGUOITAO", obj.NGUOITAO, DbType.String);
         DalTools.addDbDataParameter(command, "NGAYCAPNHAT", obj.NGAYCAPNHAT, DbType.DateTime);
         DalTools.addDbDataParameter(command, "NGUOICAPNHAT", obj.NGUOICAPNHAT, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(KHACHHANG obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "MAKHACHHANG", obj.MAKHACHHANG, DbType.Guid);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(KHACHHANG obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(KHACHHANG obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private KHACHHANGList SelectListByCommand(IDbCommand command)
        {
            KHACHHANGList list = new KHACHHANGList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				KHACHHANG obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new KHACHHANG();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private KHACHHANGList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            KHACHHANGList list = new KHACHHANGList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				KHACHHANG obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new KHACHHANG();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(KHACHHANG obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["MAKHACHHANG"] is DBNull))
		{
			obj.MAKHACHHANG = (Guid)reader["MAKHACHHANG"];
		}
                if(!(reader["MABARCODE"] is DBNull))
		{
			obj.MABARCODE = (string)reader["MABARCODE"];
		}
                if(!(reader["HINHANH"] is DBNull))
		{
			obj.HINHANH = (byte[])reader["HINHANH"];
		}
                if(!(reader["CODEKHACHHANG"] is DBNull))
		{
			obj.CODEKHACHHANG = (string)reader["CODEKHACHHANG"];
		}
                if(!(reader["CMND"] is DBNull))
		{
			obj.CMND = (string)reader["CMND"];
		}
                if(!(reader["MAKHUVUC"] is DBNull))
		{
			obj.MAKHUVUC = (Int32)reader["MAKHUVUC"];
		}
                if(!(reader["MANHOM"] is DBNull))
		{
			obj.MANHOM = (Int32)reader["MANHOM"];
		}
                if(!(reader["MALOAIKHACHHANG"] is DBNull))
		{
			obj.MALOAIKHACHHANG = (Int32)reader["MALOAIKHACHHANG"];
		}
                if(!(reader["TENKHACHHANG"] is DBNull))
		{
			obj.TENKHACHHANG = (string)reader["TENKHACHHANG"];
		}
                if(!(reader["MASOTHUE"] is DBNull))
		{
			obj.MASOTHUE = (string)reader["MASOTHUE"];
		}
                if(!(reader["NGUOIDAIDIEN"] is DBNull))
		{
			obj.NGUOIDAIDIEN = (string)reader["NGUOIDAIDIEN"];
		}
                if(!(reader["NGAYSINH"] is DBNull))
		{
			obj.NGAYSINH = (DateTime)reader["NGAYSINH"];
		}
                if(!(reader["GIOITINH"] is DBNull))
		{
			obj.GIOITINH = (string)reader["GIOITINH"];
		}
                if(!(reader["CHUCVU"] is DBNull))
		{
			obj.CHUCVU = (string)reader["CHUCVU"];
		}
                if(!(reader["DIACHI"] is DBNull))
		{
			obj.DIACHI = (string)reader["DIACHI"];
		}
                if(!(reader["DIENTHOAI"] is DBNull))
		{
			obj.DIENTHOAI = (string)reader["DIENTHOAI"];
		}
                if(!(reader["FAX"] is DBNull))
		{
			obj.FAX = (string)reader["FAX"];
		}
                if(!(reader["DIDONG"] is DBNull))
		{
			obj.DIDONG = (string)reader["DIDONG"];
		}
                if(!(reader["EMAIL"] is DBNull))
		{
			obj.EMAIL = (string)reader["EMAIL"];
		}
                if(!(reader["CHECKTPYE"] is DBNull))
		{
			obj.CHECKTPYE = (Int32)reader["CHECKTPYE"];
		}
                if(!(reader["GHICHU"] is DBNull))
		{
			obj.GHICHU = (string)reader["GHICHU"];
		}
                if(!(reader["DIACHICONGTY"] is DBNull))
		{
			obj.DIACHICONGTY = (string)reader["DIACHICONGTY"];
		}
                if(!(reader["TENTATKHACHHANG"] is DBNull))
		{
			obj.TENTATKHACHHANG = (string)reader["TENTATKHACHHANG"];
		}
                if(!(reader["CONGTY"] is DBNull))
		{
			obj.CONGTY = (string)reader["CONGTY"];
		}
                if(!(reader["DIACHIGIAOHOADON"] is DBNull))
		{
			obj.DIACHIGIAOHOADON = (string)reader["DIACHIGIAOHOADON"];
		}
                if(!(reader["DIACHIGIAOHANG"] is DBNull))
		{
			obj.DIACHIGIAOHANG = (string)reader["DIACHIGIAOHANG"];
		}
                if(!(reader["DIACHIGIAOHANG2"] is DBNull))
		{
			obj.DIACHIGIAOHANG2 = (string)reader["DIACHIGIAOHANG2"];
		}
                if(!(reader["TONGCONGNO"] is DBNull))
		{
			obj.TONGCONGNO = (decimal)reader["TONGCONGNO"];
		}
                if(!(reader["GIOIHANCONGNO"] is DBNull))
		{
			obj.GIOIHANCONGNO = (decimal)reader["GIOIHANCONGNO"];
		}
                if(!(reader["GIOIHANSONGAYNO"] is DBNull))
		{
			obj.GIOIHANSONGAYNO = (Int32)reader["GIOIHANSONGAYNO"];
		}
                if(!(reader["NGAYNOGANNHAT"] is DBNull))
		{
			obj.NGAYNOGANNHAT = (DateTime)reader["NGAYNOGANNHAT"];
		}
                if(!(reader["CHIETKHAU"] is DBNull))
		{
			obj.CHIETKHAU = (double)reader["CHIETKHAU"];
		}
                if(!(reader["CHECKKHUYENMAI"] is DBNull))
		{
			obj.CHECKKHUYENMAI = (Int32)reader["CHECKKHUYENMAI"];
		}
                if(!(reader["ISUSE"] is DBNull))
		{
			obj.ISUSE = (bool)reader["ISUSE"];
		}
                if(!(reader["ISDELETE"] is DBNull))
		{
			obj.ISDELETE = (bool)reader["ISDELETE"];
		}
                if(!(reader["NGAYTAO"] is DBNull))
		{
			obj.NGAYTAO = (DateTime)reader["NGAYTAO"];
		}
                if(!(reader["NGUOITAO"] is DBNull))
		{
			obj.NGUOITAO = (string)reader["NGUOITAO"];
		}
                if(!(reader["NGAYCAPNHAT"] is DBNull))
		{
			obj.NGAYCAPNHAT = (DateTime)reader["NGAYCAPNHAT"];
		}
                if(!(reader["NGUOICAPNHAT"] is DBNull))
		{
			obj.NGUOICAPNHAT = (string)reader["NGUOICAPNHAT"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public KHACHHANG SelectByKey(Guid MAKHACHHANG)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MAKHACHHANG", MAKHACHHANG, DbType.Guid);
      	    KHACHHANGList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public KHACHHANG SelectByKey(Guid MAKHACHHANG, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MAKHACHHANG", MAKHACHHANG, DbType.Guid);
      	    KHACHHANGList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public KHACHHANGList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    KHACHHANGList list =  SelectListByCommand(command);
	        return list;
        }

         public KHACHHANGList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    KHACHHANGList list =  SelectListByCommand(command);
	        return list;
        }

        public KHACHHANGList SelectByCondition(KHACHHANGConditionForm condt)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM KHACHHANG " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            KHACHHANGList list = SelectListByCommand(command);
            return list;
        }

        private void DeleteByCondition(KHACHHANGConditionForm condt, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM KHACHHANG " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(KHACHHANGConditionForm obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetMAKHACHHANG)
	{
    	    s += " AND MAKHACHHANG = @MAKHACHHANG ";
	}
	if (obj.IsSetMABARCODE)
	{
    	    s += " AND MABARCODE = @MABARCODE ";
	}
	if (obj.IsSetHINHANH)
	{
    	    s += " AND HINHANH = @HINHANH ";
	}
	if (obj.IsSetCODEKHACHHANG)
	{
    	    s += " AND CODEKHACHHANG = @CODEKHACHHANG ";
	}
	if (obj.IsSetCMND)
	{
    	    s += " AND CMND = @CMND ";
	}
	if (obj.IsSetMAKHUVUC)
	{
    	    s += " AND MAKHUVUC = @MAKHUVUC ";
	}
	if (obj.IsSetMANHOM)
	{
    	    s += " AND MANHOM = @MANHOM ";
	}
	if (obj.IsSetMALOAIKHACHHANG)
	{
    	    s += " AND MALOAIKHACHHANG = @MALOAIKHACHHANG ";
	}
	if (obj.IsSetTENKHACHHANG)
	{
    	    s += " AND TENKHACHHANG = @TENKHACHHANG ";
	}
	if (obj.IsSetMASOTHUE)
	{
    	    s += " AND MASOTHUE = @MASOTHUE ";
	}
	if (obj.IsSetNGUOIDAIDIEN)
	{
    	    s += " AND NGUOIDAIDIEN = @NGUOIDAIDIEN ";
	}
	if (obj.IsSetNGAYSINH)
	{
    	    s += " AND NGAYSINH = @NGAYSINH ";
	}
	if (obj.IsSetGIOITINH)
	{
    	    s += " AND GIOITINH = @GIOITINH ";
	}
	if (obj.IsSetCHUCVU)
	{
    	    s += " AND CHUCVU = @CHUCVU ";
	}
	if (obj.IsSetDIACHI)
	{
    	    s += " AND DIACHI = @DIACHI ";
	}
	if (obj.IsSetDIENTHOAI)
	{
    	    s += " AND DIENTHOAI = @DIENTHOAI ";
	}
	if (obj.IsSetFAX)
	{
    	    s += " AND FAX = @FAX ";
	}
	if (obj.IsSetDIDONG)
	{
    	    s += " AND DIDONG = @DIDONG ";
	}
	if (obj.IsSetEMAIL)
	{
    	    s += " AND EMAIL = @EMAIL ";
	}
	if (obj.IsSetCHECKTPYE)
	{
    	    s += " AND CHECKTPYE = @CHECKTPYE ";
	}
	if (obj.IsSetGHICHU)
	{
    	    s += " AND GHICHU = @GHICHU ";
	}
	if (obj.IsSetDIACHICONGTY)
	{
    	    s += " AND DIACHICONGTY = @DIACHICONGTY ";
	}
	if (obj.IsSetTENTATKHACHHANG)
	{
    	    s += " AND TENTATKHACHHANG = @TENTATKHACHHANG ";
	}
	if (obj.IsSetCONGTY)
	{
    	    s += " AND CONGTY = @CONGTY ";
	}
	if (obj.IsSetDIACHIGIAOHOADON)
	{
    	    s += " AND DIACHIGIAOHOADON = @DIACHIGIAOHOADON ";
	}
	if (obj.IsSetDIACHIGIAOHANG)
	{
    	    s += " AND DIACHIGIAOHANG = @DIACHIGIAOHANG ";
	}
	if (obj.IsSetDIACHIGIAOHANG2)
	{
    	    s += " AND DIACHIGIAOHANG2 = @DIACHIGIAOHANG2 ";
	}
	if (obj.IsSetTONGCONGNO)
	{
    	    s += " AND TONGCONGNO = @TONGCONGNO ";
	}
	if (obj.IsSetGIOIHANCONGNO)
	{
    	    s += " AND GIOIHANCONGNO = @GIOIHANCONGNO ";
	}
	if (obj.IsSetGIOIHANSONGAYNO)
	{
    	    s += " AND GIOIHANSONGAYNO = @GIOIHANSONGAYNO ";
	}
	if (obj.IsSetNGAYNOGANNHAT)
	{
    	    s += " AND NGAYNOGANNHAT = @NGAYNOGANNHAT ";
	}
	if (obj.IsSetCHIETKHAU)
	{
    	    s += " AND CHIETKHAU = @CHIETKHAU ";
	}
	if (obj.IsSetCHECKKHUYENMAI)
	{
    	    s += " AND CHECKKHUYENMAI = @CHECKKHUYENMAI ";
	}
	if (obj.IsSetISUSE)
	{
    	    s += " AND ISUSE = @ISUSE ";
	}
	if (obj.IsSetISDELETE)
	{
    	    s += " AND ISDELETE = @ISDELETE ";
	}
	if (obj.IsSetNGAYTAO)
	{
    	    s += " AND NGAYTAO = @NGAYTAO ";
	}
	if (obj.IsSetNGUOITAO)
	{
    	    s += " AND NGUOITAO = @NGUOITAO ";
	}
	if (obj.IsSetNGAYCAPNHAT)
	{
    	    s += " AND NGAYCAPNHAT = @NGAYCAPNHAT ";
	}
	if (obj.IsSetNGUOICAPNHAT)
	{
    	    s += " AND NGUOICAPNHAT = @NGUOICAPNHAT ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, KHACHHANGConditionForm obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetMAKHACHHANG)
            {
            	DalTools.addDbDataParameter(command, "MAKHACHHANG", obj.MAKHACHHANG, DbType.Guid); 
            }
            if (obj.IsSetMABARCODE)
            {
            	DalTools.addDbDataParameter(command, "MABARCODE", obj.MABARCODE, DbType.String); 
            }
            if (obj.IsSetHINHANH)
            {
            	DalTools.addDbDataParameter(command, "HINHANH", obj.HINHANH, DbType.Binary); 
            }
            if (obj.IsSetCODEKHACHHANG)
            {
            	DalTools.addDbDataParameter(command, "CODEKHACHHANG", obj.CODEKHACHHANG, DbType.String); 
            }
            if (obj.IsSetCMND)
            {
            	DalTools.addDbDataParameter(command, "CMND", obj.CMND, DbType.String); 
            }
            if (obj.IsSetMAKHUVUC)
            {
            	DalTools.addDbDataParameter(command, "MAKHUVUC", obj.MAKHUVUC, DbType.Int32); 
            }
            if (obj.IsSetMANHOM)
            {
            	DalTools.addDbDataParameter(command, "MANHOM", obj.MANHOM, DbType.Int32); 
            }
            if (obj.IsSetMALOAIKHACHHANG)
            {
            	DalTools.addDbDataParameter(command, "MALOAIKHACHHANG", obj.MALOAIKHACHHANG, DbType.Int32); 
            }
            if (obj.IsSetTENKHACHHANG)
            {
            	DalTools.addDbDataParameter(command, "TENKHACHHANG", obj.TENKHACHHANG, DbType.String); 
            }
            if (obj.IsSetMASOTHUE)
            {
            	DalTools.addDbDataParameter(command, "MASOTHUE", obj.MASOTHUE, DbType.String); 
            }
            if (obj.IsSetNGUOIDAIDIEN)
            {
            	DalTools.addDbDataParameter(command, "NGUOIDAIDIEN", obj.NGUOIDAIDIEN, DbType.String); 
            }
            if (obj.IsSetNGAYSINH)
            {
            	DalTools.addDbDataParameter(command, "NGAYSINH", obj.NGAYSINH, DbType.DateTime); 
            }
            if (obj.IsSetGIOITINH)
            {
            	DalTools.addDbDataParameter(command, "GIOITINH", obj.GIOITINH, DbType.String); 
            }
            if (obj.IsSetCHUCVU)
            {
            	DalTools.addDbDataParameter(command, "CHUCVU", obj.CHUCVU, DbType.String); 
            }
            if (obj.IsSetDIACHI)
            {
            	DalTools.addDbDataParameter(command, "DIACHI", obj.DIACHI, DbType.String); 
            }
            if (obj.IsSetDIENTHOAI)
            {
            	DalTools.addDbDataParameter(command, "DIENTHOAI", obj.DIENTHOAI, DbType.String); 
            }
            if (obj.IsSetFAX)
            {
            	DalTools.addDbDataParameter(command, "FAX", obj.FAX, DbType.String); 
            }
            if (obj.IsSetDIDONG)
            {
            	DalTools.addDbDataParameter(command, "DIDONG", obj.DIDONG, DbType.String); 
            }
            if (obj.IsSetEMAIL)
            {
            	DalTools.addDbDataParameter(command, "EMAIL", obj.EMAIL, DbType.String); 
            }
            if (obj.IsSetCHECKTPYE)
            {
            	DalTools.addDbDataParameter(command, "CHECKTPYE", obj.CHECKTPYE, DbType.Int32); 
            }
            if (obj.IsSetGHICHU)
            {
            	DalTools.addDbDataParameter(command, "GHICHU", obj.GHICHU, DbType.String); 
            }
            if (obj.IsSetDIACHICONGTY)
            {
            	DalTools.addDbDataParameter(command, "DIACHICONGTY", obj.DIACHICONGTY, DbType.String); 
            }
            if (obj.IsSetTENTATKHACHHANG)
            {
            	DalTools.addDbDataParameter(command, "TENTATKHACHHANG", obj.TENTATKHACHHANG, DbType.String); 
            }
            if (obj.IsSetCONGTY)
            {
            	DalTools.addDbDataParameter(command, "CONGTY", obj.CONGTY, DbType.String); 
            }
            if (obj.IsSetDIACHIGIAOHOADON)
            {
            	DalTools.addDbDataParameter(command, "DIACHIGIAOHOADON", obj.DIACHIGIAOHOADON, DbType.String); 
            }
            if (obj.IsSetDIACHIGIAOHANG)
            {
            	DalTools.addDbDataParameter(command, "DIACHIGIAOHANG", obj.DIACHIGIAOHANG, DbType.String); 
            }
            if (obj.IsSetDIACHIGIAOHANG2)
            {
            	DalTools.addDbDataParameter(command, "DIACHIGIAOHANG2", obj.DIACHIGIAOHANG2, DbType.String); 
            }
            if (obj.IsSetTONGCONGNO)
            {
            	DalTools.addDbDataParameter(command, "TONGCONGNO", obj.TONGCONGNO, DbType.Decimal); 
            }
            if (obj.IsSetGIOIHANCONGNO)
            {
            	DalTools.addDbDataParameter(command, "GIOIHANCONGNO", obj.GIOIHANCONGNO, DbType.Decimal); 
            }
            if (obj.IsSetGIOIHANSONGAYNO)
            {
            	DalTools.addDbDataParameter(command, "GIOIHANSONGAYNO", obj.GIOIHANSONGAYNO, DbType.Int32); 
            }
            if (obj.IsSetNGAYNOGANNHAT)
            {
            	DalTools.addDbDataParameter(command, "NGAYNOGANNHAT", obj.NGAYNOGANNHAT, DbType.DateTime); 
            }
            if (obj.IsSetCHIETKHAU)
            {
            	DalTools.addDbDataParameter(command, "CHIETKHAU", obj.CHIETKHAU, DbType.Double); 
            }
            if (obj.IsSetCHECKKHUYENMAI)
            {
            	DalTools.addDbDataParameter(command, "CHECKKHUYENMAI", obj.CHECKKHUYENMAI, DbType.Int32); 
            }
            if (obj.IsSetISUSE)
            {
            	DalTools.addDbDataParameter(command, "ISUSE", obj.ISUSE, DbType.Boolean); 
            }
            if (obj.IsSetISDELETE)
            {
            	DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean); 
            }
            if (obj.IsSetNGAYTAO)
            {
            	DalTools.addDbDataParameter(command, "NGAYTAO", obj.NGAYTAO, DbType.DateTime); 
            }
            if (obj.IsSetNGUOITAO)
            {
            	DalTools.addDbDataParameter(command, "NGUOITAO", obj.NGUOITAO, DbType.String); 
            }
            if (obj.IsSetNGAYCAPNHAT)
            {
            	DalTools.addDbDataParameter(command, "NGAYCAPNHAT", obj.NGAYCAPNHAT, DbType.DateTime); 
            }
            if (obj.IsSetNGUOICAPNHAT)
            {
            	DalTools.addDbDataParameter(command, "NGUOICAPNHAT", obj.NGUOICAPNHAT, DbType.String); 
            }
        }
        #endregion
    }
}